package com.cr.admin.business.topic.dao.repository;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.admin.business.topic.dao.mapper.TopicMapper;
import com.cr.admin.business.topic.pojo.entity.Topic;
import com.cr.admin.business.topic.pojo.param.TopicAddParam;
import com.cr.admin.business.topic.pojo.param.TopicUpdateParam;
import com.cr.admin.business.topic.pojo.query.TopicQuery;
import com.cr.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TopicRepositoryImpl implements TopicRepository{
    @Autowired
    private TopicMapper topicMapper;

    @Override
    public List<Topic> selectList(TopicQuery topicQuery) {
        QueryWrapper<Topic> queryWrapper= new QueryWrapper<>();

        if(topicQuery.getTopicTitle()!=null){
            queryWrapper.like("topic_title",topicQuery.getTopicTitle());
        }
        if(topicQuery.getContent()!=null){
            queryWrapper.like("content",topicQuery.getContent());
        }

        return topicMapper.selectList(queryWrapper);


    }

    @Override
    public void insert(TopicAddParam topicAddParam) {
        Topic topic=new Topic();
        BeanUtils.copyProperties(topicAddParam,topic);
        topicMapper.insert(topic);
    }

    @Override
    public void updateById(TopicUpdateParam topicUpdateParam) {
        Topic topic=new Topic();
        BeanUtils.copyProperties(topicUpdateParam,topic);
        topicMapper.updateById(topic);
    }

    @Override
    public Topic selectById(Long id) {

        return topicMapper.selectById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        topicMapper.deleteBatchIds(ids);
    }
}
